package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.pojo.query.ProductQuery;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    // A -> B  100
    @Override
    public LayUITableResult list(ProductQuery productQuery) {
        //开启分页
        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        // 开启分页之后，查找所有后面会自动拼接上分页相关信息limit
        List<Product> list = productMapper.list(productQuery);
        //查找一共有多少数量
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();

        return LayUITableResult.ok((int)totalCount, list);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        productMapper.deleteAll(ids);
    }

    @Override
    public void add(Product product) {
        productMapper.insertSelective(product);
    }
}
